/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.alimuzaffar.sample.pin;

import com.alimuzaffar.lib.pin.PinEntryEditText;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.utils.PacMap;
import ohos.vibrator.agent.VibratorAgent;
import ohos.vibrator.bean.VibrationPattern;

import java.util.List;

/**
 * AnimatedEditTextWidgetsAbility
 *
 * @since 2021-07-15
 */
public class AnimatedEditTextWidgetsAbility extends Ability {
    private static final int DELAY_TIME = 1000;
    private EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());

    private VibratorAgent mVibratorAgent;
    private List<Integer> mVibratorList;
    private Integer mVibratorId;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_edit_text);

        if (mVibratorAgent == null) {
            mVibratorAgent = new VibratorAgent();
        }
        mVibratorList = mVibratorAgent.getVibratorIdList();
        if (!mVibratorList.isEmpty()) {
            mVibratorId = mVibratorList.get(0);
        }

        initView();
        initViewTwo();
    }

    private void initView() {
        PinEntryEditText pinEntry = (PinEntryEditText)findComponentById(ResourceTable.Id_txt_pin_entry);
        if(pinEntry != null) {
            pinEntry.setLongClickable(true);
            pinEntry.setLongClickedListener(new Component.LongClickedListener() {
                @Override
                public void onLongClicked(Component component) {
                    // 震动反馈
                    mVibratorAgent.startOnce(mVibratorId,
                        VibrationPattern.VIBRATOR_TYPE_CONTROL_TEXT_EDIT);
                }
            });

            // 设置默认获取到焦点
            pinEntry.requestFocus();
            pinEntry.setOnPinEnteredListener(new PinEntryEditText.OnPinEnteredListener() {
                @Override
                public void onPinEntered(CharSequence str) {
                    if (str.toString().equals("1234")) {
                        Toast.showShort(AnimatedEditTextWidgetsAbility.this, "SUCCESS");
                    } else {
                        pinEntry.setError(true);
                        Toast.showShort(AnimatedEditTextWidgetsAbility.this, "FAIL");
                        eventHandler.postTask(new Runnable() {
                            @Override
                            public void run() {
                                pinEntry.setText("");
                                pinEntry.setError(false);
                            }
                        }, DELAY_TIME);

                    }
                }
            });

        }
    }

    private void initViewTwo() {
        PinEntryEditText pinEntry2 = (PinEntryEditText)findComponentById(ResourceTable.Id_txt_pin_entry2);
        if (pinEntry2 != null) {
            pinEntry2.setOnPinEnteredListener(new PinEntryEditText.OnPinEnteredListener() {
                @Override
                public void onPinEntered(CharSequence str) {
                    if (str.toString().equals("1234")) {
                        Toast.showShort(AnimatedEditTextWidgetsAbility.this, "SUCCESS");
                        pinEntry2.setText("");
                        pinEntry2.setError(false);
                    } else {
                        pinEntry2.setError(true);
                        Toast.showShort(AnimatedEditTextWidgetsAbility.this, "FAIL");
                        eventHandler.postTask(new Runnable() {
                            @Override
                            public void run() {
                                pinEntry2.setText("");
                                pinEntry2.setError(false);
                            }
                        }, DELAY_TIME);
                    }
                }
            });
            pinEntry2.setLongClickable(true);
            pinEntry2.setLongClickedListener(new Component.LongClickedListener() {
                @Override
                public void onLongClicked(Component component) {
                    // 震动反馈
                    mVibratorAgent.startOnce(mVibratorId,
                        VibrationPattern.VIBRATOR_TYPE_CONTROL_TEXT_EDIT);

                }
            });
        }
    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);
    }

    @Override
    public void onRestoreAbilityState(PacMap inState) {
        super.onRestoreAbilityState(inState);
    }
}
